#include "CMarioSound.h"
bool CMarioSound::flag = false;
CMarioSound *CMarioSound::_instance = NULL;

CMarioSound::CMarioSound()
{
	dsound = new CSoundManager();

}

CMarioSound* CMarioSound::getInstance()
{
	if (!flag)
	{
		_instance = new CMarioSound();
		flag = true;
	}
	return _instance;
}


CMarioSound::~CMarioSound()
{
	/*if (SoundJump != NULL)
		delete SoundJump;
	if (SoundDie != NULL)
		delete SoundDie;
	if (SoundStart != NULL)
		delete SoundStart;*/

}

int CMarioSound::InitSound(HWND hWnd)
{
	HRESULT result;
	result = dsound->Initialize(hWnd,DSSCL_PRIORITY);
	if (result != DS_OK)
		return 0;

	result = dsound->SetPrimaryBufferFormat(2,22050,16);
	if (result != DS_OK)
		return 0;

	//jump
	SoundJump = LoadWave("Sounds\\jump.wav");
	if (SoundJump == NULL) 
		return 0;
	
	//background
	SoundStart = LoadWave("Sounds\\Mario Riff.wav");
	if (SoundStart == NULL) return 0;

	//die
	SoundDie = LoadWave("Sounds\\Dying.wav");
	if (SoundDie == NULL) return 0;

	SoundGetCoin=LoadWave("Sounds\\GetCoin.wav");
	if(SoundGetCoin==NULL)return 0;

	//gameover
	SoundGameOver=LoadWave("Sounds\\GameOver.wav");
	if(SoundGameOver==NULL)return 0;

	//backbround
	//SoundBackground=LoadWave("Sounds\\BackMusic.wav");
	/*SoundBackground=LoadWave("Sounds\\Background.wav");
	if(SoundBackground==NULL)return 0;*/

	SoundMenu=LoadWave("Sounds\\MenuMusic.wav");
	if(SoundMenu==NULL)return 0;
	//ExtraLife
	SoundExtraLife=LoadWave("Sounds\\ExtraLife.wav");
	if(SoundExtraLife==NULL)return 0;

	//EnemyDie
	SoundEnemyDie=LoadWave("Sounds\\EnemyDie.wav");
	if(SoundEnemyDie==NULL)return 0;

	//BrickBroken
	SoundBrickBroken=LoadWave("Sounds\\BrickBroken.wav");
	if(SoundBrickBroken==NULL)return 0;
	
	//FinishMap
	SoundLevelComplete=LoadWave("Sounds\\FinishMap.wav");
	if(SoundLevelComplete==NULL) return 0;

	//ChangMenu
	SoundMenuChange=LoadWave("Sounds\\MenuChange.wav");
	if(SoundMenuChange==NULL)return 0;

	//Bonus Appear
	SoundBonusAppear=LoadWave("Sounds\\BonusAppeared.wav");
	if(SoundBonusAppear==NULL)return 0;

	//Sound Shoot
	SoundShoot=LoadWave("Sounds\\Shoot.wav");
	if(SoundShoot==NULL)return 0;

	//Sound Level Up
	SoundLevelUp=LoadWave("Sounds\\LevelUp.wav");
	if(SoundLevelUp==NULL)return 0;

	return 1;

}

CSound* CMarioSound::LoadWave(LPTSTR filename)
{
	HRESULT result;
	CSound* wavestream;
	result = dsound->Create(&wavestream,filename);
	if (result != DS_OK)
		return NULL;

	return wavestream;
}

void CMarioSound::Play(int index, bool isLooping)
{
	switch (index)
	{
	case 1:
		{
			if (!isLooping)
				SoundStart->Play();
			else SoundStart->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case 0:
		{
		if (!isLooping)
			SoundJump->Play();
		else SoundJump->Play(0,DSBPLAY_LOOPING);
		break;
		}
	case 2:
		{
			if (!isLooping)
			SoundDie->Play();
			else SoundDie->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case 3:
		{
			if(!isLooping)
				SoundGetCoin->Play();
			else SoundGetCoin->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case 4:
		{
			/*if(!isLooping)
				SoundBackground->Play();
			else SoundBackground->Play(0,DSBPLAY_LOOPING);*/
			break;
		}
	case 5:
		{
			if(!isLooping)
				SoundMenu->Play();
			else SoundMenu->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case 6:
		{
			if(!isLooping)
				SoundExtraLife->Play();
			else SoundExtraLife->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case 7:
		{
			if(!isLooping)
				SoundEnemyDie->Play();
			else SoundEnemyDie->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case 8:
		{
			if(!isLooping)
				SoundBrickBroken->Play();
			else SoundBrickBroken->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case 9:
		{
			if(!isLooping)
				SoundLevelComplete->Play();
			else SoundLevelComplete->Play(0,DSBPLAY_LOOPING);
			break;
		}

	case 11:
		{
			if(!isLooping)
				SoundGameOver->Play();
			else SoundGameOver->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case 12:
		{
		if(!isLooping)
				SoundMenuChange->Play();
		else SoundMenuChange->Play(0,DSBPLAY_LOOPING);
		break;
		}

	case SOUND_BONUSAPPEARED:
		{
			if(!isLooping)
				SoundBonusAppear->Play();
			else SoundBonusAppear->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case SOUND_SHOOT:
		{
			if(!isLooping)
				SoundShoot->Play();
			else SoundShoot->Play(0,DSBPLAY_LOOPING);
			break;
		}
	case SOUND_LEVELUP:
		{
			if(!isLooping)
				SoundLevelUp->Play();
			else SoundLevelUp->Play(0,DSBPLAY_LOOPING);
			break;
		}
	}
		
	
}

void CMarioSound::Stop(int index)
{
	switch (index)
	{
	case 0:
		SoundJump->Stop();
		break;
	case 1:
		SoundStart->Stop();
		break;
	case 2: 
		SoundDie->Stop();
		break;
	case 3:
		SoundGetCoin->Stop();
		break;
	case 4:
		//SoundBackground->Stop();
		break;
	case 5:
		SoundMenu->Stop();
		break;
	case 6:
		SoundExtraLife->Stop();
		break;
	case 7:
		SoundEnemyDie->Stop();
		break;
	case 8:
		SoundBrickBroken->Stop();
		break;
	case 9:
		SoundLevelComplete->Stop();
		break;

	case 11:
		SoundGameOver->Stop();
		break;
	case 12:
		SoundMenuChange->Stop();
		break;
	}
	
}
void CMarioSound::StopExceptionSound(int index)
{
	
}
